package ipmsg.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ipmsg.etc.Command;
import ipmsg.etc.FileLinkList;
import ipmsg.etc.FileNode;

public class SendFiles extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1092197232596227921L;

	private String desIp;
	private String note = null;
	private JButton guiAdd;
	private JList<String> guiAllFiles;

	public SendFiles(String desIp) {
		init(desIp);
	}

	public SendFiles(String remoteIP, File[] files) {
		init(remoteIP);
		addFiles(files);
	}

	private JButton guiCancel;
	private JButton guiDel;
	private JCheckBox guiSelectALL;
	private JButton guiSend;
	private JButton jButton1;
	private JMenu jMenu1;
	private javax.swing.JMenuBar jMenuBar1;

	private void initComponents() {
		this.jPanel1 = new JPanel();
		this.guiSend = new JButton();
		this.guiCancel = new JButton();
		this.guiAdd = new JButton();
		this.guiDel = new JButton();
		this.guiSelectALL = new JCheckBox();
		this.jButton1 = new JButton();
		this.jPanel2 = new JPanel();
		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.guiAllFiles = new JList<String>();
		this.jMenuBar1 = new javax.swing.JMenuBar();
		this.jMenu1 = new JMenu();
		this.jMenuItem1 = new JMenuItem();
		this.jMenuItem2 = new JMenuItem();
		this.jMenuItem3 = new JMenuItem();
		this.jSeparator1 = new javax.swing.JSeparator();
		this.jMenuItem5 = new JMenuItem();
		this.jMenuItem4 = new JMenuItem();

		setDefaultCloseOperation(2);

		this.guiSend.setText("发送");
		this.guiSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.guiSendActionPerformed(evt);
			}

		});
		this.guiCancel.setText("退出");
		this.guiCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.guiCancelActionPerformed(evt);
			}

		});
		this.guiAdd.setText("添加");
		this.guiAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.guiAddActionPerformed(evt);
			}

		});
		this.guiDel.setText("删除");
		this.guiDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.guiDelActionPerformed(evt);
			}

		});
		this.guiSelectALL.setText("全选");
		this.guiSelectALL.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				SendFiles.this.guiSelectALLItemStateChanged(evt);
			}

		});
		this.jButton1.setText("添加说明");
		this.jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jButton1ActionPerformed(evt);
			}

		});
		GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
		this.jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(this.guiSelectALL)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.guiAdd)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.guiDel)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, 32767)
						.addComponent(this.jButton1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(this.guiCancel)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.guiSend)
						.addContainerGap()));

		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				GroupLayout.Alignment.TRAILING,
				jPanel1Layout.createSequentialGroup().addContainerGap(-1, 32767)
						.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(this.guiAdd).addComponent(this.guiDel).addComponent(this.guiSelectALL)
								.addComponent(this.guiSend).addComponent(this.guiCancel).addComponent(this.jButton1))
						.addContainerGap()));

		getContentPane().add(this.jPanel1, "South");

		this.guiAllFiles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				SendFiles.this.guiAllFilesValueChanged(evt);
			}
		});
		this.jScrollPane1.setViewportView(this.guiAllFiles);

		GroupLayout jPanel2Layout = new GroupLayout(this.jPanel2);
		this.jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(this.jScrollPane1, -1, 427, 32767));

		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(this.jScrollPane1, -1, 221, 32767));

		getContentPane().add(this.jPanel2, "Center");

		this.jMenu1.setText("文件");

		this.jMenuItem1.setText("添加文件");
		this.jMenuItem1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jMenuItem1ActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.jMenuItem1);

		this.jMenuItem2.setText("删除文件");
		this.jMenuItem2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jMenuItem2ActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.jMenuItem2);

		this.jMenuItem3.setText("清空列表");
		this.jMenuItem3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jMenuItem3ActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.jMenuItem3);
		this.jMenu1.add(this.jSeparator1);

		this.jMenuItem5.setText("发送");
		this.jMenuItem5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jMenuItem5ActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.jMenuItem5);

		this.jMenuItem4.setText("退出");
		this.jMenuItem4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SendFiles.this.jMenuItem4ActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.jMenuItem4);

		this.jMenuBar1.add(this.jMenu1);

		setJMenuBar(this.jMenuBar1);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 435) / 2, (screenSize.height - 318) / 2, 435, 318);
	}

	private void guiSendActionPerformed(ActionEvent evt) {
		sendFiles();
	}

	private void guiCancelActionPerformed(ActionEvent evt) {
		dispose();
	}

	private void guiAddActionPerformed(ActionEvent evt) {
		File[] files;
		if ((files = UtilityGui.chooseFile(this)) != null) {
			addFiles(files);
		}
	}

	private void guiDelActionPerformed(ActionEvent evt) {
		delFiles();
	}

	private void guiSelectALLItemStateChanged(java.awt.event.ItemEvent evt) {
		if (this.guiSelectALL.isSelected()) {
			int len;
			if ((len = this.guiAllFiles.getModel().getSize()) > 0) {
				int[] indices = new int[len];
				for (int i = 0; i < len; i++)
					indices[i] = i;
				this.guiAllFiles.setSelectedIndices(indices);
			}
		} else {
			this.guiAllFiles.clearSelection();
		}
	}

	private void guiAllFilesValueChanged(javax.swing.event.ListSelectionEvent evt) {
		int selectLen = this.guiAllFiles.getSelectedIndices().length;
		if (selectLen > 0)
			this.guiDel.setEnabled(true);
		else {
			this.guiDel.setEnabled(false);
		}
	}

	private void jMenuItem1ActionPerformed(ActionEvent evt) {
		File[] files;
		if ((files = UtilityGui.chooseFile(this)) != null) {
			addFiles(files);
		}
	}

	private void jMenuItem2ActionPerformed(ActionEvent evt) {
		delFiles();
	}

	private void jMenuItem3ActionPerformed(ActionEvent evt) {
		this.guiAllFiles.setModel(new DefaultListModel<String>());
	}

	private void jMenuItem5ActionPerformed(ActionEvent evt) {
		sendFiles();
	}

	private void jMenuItem4ActionPerformed(ActionEvent evt) {
		dispose();
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		this.note = UtilityGui.getInputString(this, "输入您的说明", "添加说明信息", 1000);
	}

	private JMenuItem jMenuItem1;

	private JMenuItem jMenuItem2;

	private JMenuItem jMenuItem3;

	private JMenuItem jMenuItem4;

	private JMenuItem jMenuItem5;

	private JPanel jPanel1;

	private JPanel jPanel2;

	private javax.swing.JScrollPane jScrollPane1;

	private javax.swing.JSeparator jSeparator1;

	private void init(String desIp) {
		this.desIp = desIp;
		initComponents();
		this.guiDel.setEnabled(false);
		setTitle("发送文件至" + ipmsg.etc.GlobalVar.getUser(this.desIp).getName() + " ( " + this.desIp + " ) ");

		MyTransferHandler handler = new MyTransferHandler(this.guiAllFiles);
		setTransferHandler(handler);
		this.guiAllFiles.setTransferHandler(handler);
	}

	private void addFiles(File[] files) {
		if (files == null)
			return;
		DefaultListModel<String> listModel;
		if (this.guiAllFiles.getModel().getSize() == 0) {
			listModel = new DefaultListModel<String>();
		} else {
			listModel = (DefaultListModel) this.guiAllFiles.getModel();
		}
		for (int i = files.length - 1; i >= 0; i--) {
			if (!listModel.contains(files[i].getPath())) {
				listModel.addElement(files[i].getPath());
			}
		}
		this.guiAllFiles.setModel(listModel);
		this.guiDel.setEnabled(true);

		if (this.guiAllFiles.getModel().getSize() > 0) {
			this.guiAllFiles.setSelectedIndex(0);
		}
	}

	private void delFiles() {
		int[] o = this.guiAllFiles.getSelectedIndices();
		if (o.length == 0) {
			return;
		}
		DefaultListModel<String> listModel = (DefaultListModel) this.guiAllFiles.getModel();

		for (int i = o.length - 1; i >= 0; i--) {
			listModel.remove(o[i]);
		}
		this.guiAllFiles.setModel(listModel);

		if (this.guiAllFiles.getModel().getSize() == 0) {
			this.guiDel.setEnabled(false);
		}
		if (this.guiAllFiles.getModel().getSize() > 0) {
			this.guiAllFiles.setSelectedIndex(0);
		}
	}

	private void sendFiles() {
		if (this.guiAllFiles.getModel().getSize() == 0) {
			UtilityGui.showNotice(this, "请添加文件");
			return;
		}

		Command com = new Command(2097184);

		FileLinkList flist = new FileLinkList();
		flist.setIp(this.desIp);
		flist.setPacketNo(com.getPacketNo());

		DefaultListModel<String> listModel = (DefaultListModel) this.guiAllFiles.getModel();

		for (int i = 0; i < listModel.getSize();) {
			File file = new File(((String) listModel.get(i)).trim());
			FileNode fnode = new FileNode();
			if (file.isFile()) {
				fnode.setFileNo(i);
				fnode.setFileName(file.getPath());
				fnode.setFileLen(file.length());
				fnode.setFileKind(1);
				flist.getFiles().add(fnode);
				i++;
			} else if (file.isDirectory()) {
				fnode.setFileNo(i);
				fnode.setFileName(file.getPath());
				fnode.setFileLen(file.length());
				fnode.setFileKind(2);
				flist.getFiles().add(fnode);
				i++;
			}
		}
		if (this.note == null)
			this.note = "";
		com.setIp(this.desIp);
		com.setAdditional(this.note + "\000" + flist.exportPacket());
		ipmsg.network.UtilityNet.sendUdpPacket(com);
		ipmsg.etc.GlobalVar.addFileList(flist);
		dispose();
	}
}
